<template>
  <div class="singleImageUpload2" style="float:right;">
    <el-upload
      ref="upload"
      :field-name="fieldName"
      :name="name"
      :action="action"
      :headers="headers"
      :multiple="false"
      :limit="1"
      :before-upload="beforeUpload"
      :on-success="onSuccess"
      :on-error="onError"
      :on-remove="onRemove"
      :file-list="fileList"
      :on-exceed="onExceed"
      :disabled="disabled"
      show-file-list
      :drag="displayType !== 'button'"
      class="image-uploader"
    >
      <el-button v-if="displayType === 'button'" style="margin-left:auto;" size="small" type="primary">载入文件</el-button>
      <div v-else class="upload-container">
        <i class="el-icon-upload" />
        <div v-if="fileList.length === 0" class="el-upload__text">
          请将文件拖入或
          <em>载入文件</em>
        </div>
        <div v-else class="el-upload__text">
          文件已上传
        </div>
      </div>
    </el-upload>
  </div>
</template>

<script>
import { getToken } from '@/utils/auth'

export default {
  name: 'FileUpload',
  props: {
    displayType: {
      type: String,
      default() {
        return 'button'
      }
    },
    fieldName: {
      type: String,
      default() {
        return ''
      }
    },
    fileList: {
      type: Array,
      default() {
        return []
      }
    },
    disabled: {
      type: Boolean,
      default: false
    },
    action: {
      type: String,
      default() {
        return `${process.env.VUE_APP_BASE_API}/file/upload`
      }
    },
    name: {
      type: String,
      default() {
        return 'segment'
      }
    }
  },
  data() {
    return {}
  },
  computed: {
    headers() {
      return {
        token: `${getToken()}`
      }
    }
  },
  methods: {
    onRemove() {
      this.$message({
        message: '文件删除成功',
        type: 'success'
      })
      this.$emit('onRemove')
    },
    onExceed() {
      this.$message({
        message: '每次只能上传一个文件',
        type: 'warning'
      })
    },
    beforeUpload(file) {
      this.$emit('beforeUpload', file)
    },
    onSuccess(response, file) {
      this.$refs.upload.clearFiles()
      this.$emit('onSuccess', response, file)
      // const { errorCode, msg, data } = response
      // if (errorCode !== 0) {
      //   this.$message({
      //     message: (msg && `上传失败，失败原因：${msg}`) || '上传失败',
      //     type: 'error'
      //   })
      //   this.$emit('onError', data)
      // } else {
      //   this.$message({
      //     message: '上传成功',
      //     type: 'success'
      //   })
      //   this.$emit('onSuccess', data, this.fieldName)
      // }
    },
    onError(err) {
      console.log(err)
      const errMsg = (err.message && JSON.parse(err.message)) || '上传失败'
      this.$message({
        message: (errMsg.msg && `上传失败，失败原因：${errMsg.msg}`) || '上传失败',
        type: 'error'
      })
      this.$emit('onError', err)
    }
  }
}
</script>

<style lang="scss" scoped>
  .upload-container{
    width:100%;
    height: 100%;
    position: relative;
    .image-uploader{
      height: 100%;
    }
  }
</style>
